﻿using Futaba.SBus.Configuration;
using Futaba.SBus.Readers;
using Futaba.SBus.SampleApplication.TelemetryLiveView;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Futaba.SBus.SampleApplication
{
	partial class MainViewModel : NotifyPropertyChangedBase
	{

		SbusParsingService _parsingService;

		public MainViewModel()
		{
			_parsingService = new SbusParsingService();
			_parsingService.PropertyChanged += (o, e) =>
			{
				if (e.PropertyName == "IsRunning")
				{
					ConnectButtonLabel = _parsingService.IsRunning ? "Disconnect" : "Connect";
				}
			};

			using (var stream = GetType().Assembly.GetManifestResourceStream(typeof(MainWindow).Namespace + ".example_model_configuration.xml"))
			{
				Configuration = ModelConfigurationFactory.DefaultInstance.Load(stream);
			}

			LoadAvailableComPorts();
		}

		public SbusParsingService ParsingService { get { return _parsingService; } }

		private List<ComPortInformation> _internalComPorts;
		public List<ComPortInformation> ComPorts
		{
			get { return _internalComPorts; }
			set
			{
				if (Equals(_internalComPorts, value))
					return;
				_internalComPorts = value;
				RaisePropertyChanged(); //RaisePropertyChanged("ComPorts");
			}
		}

		private ComPortInformation _internalSelectedComPort;
		public ComPortInformation SelectedComPort
		{
			get { return _internalSelectedComPort; }
			set
			{
				if (Equals(_internalSelectedComPort, value))
					return;
				_internalSelectedComPort = value;
				_connectCommand.RaiseCanExecuteChanged();
				RaisePropertyChanged(); //RaisePropertyChanged("SelectedComPort");
			}
		}

		private ModelConfiguration _internalConfiguration;
		public ModelConfiguration Configuration
		{
			get { return _internalConfiguration; }
			set
			{
				if (Equals(_internalConfiguration, value))
					return;
				_internalConfiguration = value;
				RaisePropertyChanged(); //RaisePropertyChanged("Configuration");
				UpdateAllViewModels();
			}
		}

		private TelemetryLiveView.ViewModel _internalTelemetryLiveViewModel;
		public TelemetryLiveView.ViewModel TelemetryLiveViewModel
		{
			get { return _internalTelemetryLiveViewModel; }
			set
			{
				if (Equals(_internalTelemetryLiveViewModel, value))
					return;
				_internalTelemetryLiveViewModel = value;
				RaisePropertyChanged(); //RaisePropertyChanged("TelemetryLiveViewModel");
			}
		}

		private ChannelList.ViewModel _internalChannelListViewModel;
		public ChannelList.ViewModel ChannelListViewModel
		{
			get { return _internalChannelListViewModel; }
			set
			{
				if (Equals(_internalChannelListViewModel, value))
					return;
				_internalChannelListViewModel = value;
				RaisePropertyChanged(); //RaisePropertyChanged("ChannelListViewModel");
			}
		}

		private string _internalConnectButtonLabel = "Connect";
		public string ConnectButtonLabel
		{
			get { return _internalConnectButtonLabel; }
			set
			{
				if (Equals(_internalConnectButtonLabel, value))
					return;
				_internalConnectButtonLabel = value;
				RaisePropertyChanged(); //RaisePropertyChanged("ConnectButtonLabel");
			}
		}

		private void UpdateAllViewModels()
		{
			if (TelemetryLiveViewModel == null)
				TelemetryLiveViewModel = new TelemetryLiveView.ViewModel();

			if (ChannelListViewModel == null)
				ChannelListViewModel = new ChannelList.ViewModel();

			TelemetryLiveViewModel.Slots = Configuration
				.Slots.Select(s => new SlotWithHistory(s)).ToArray();
			ChannelListViewModel.Channels = Configuration.Channels;

			_parsingService.ChannelParser.ItemsCollection = ChannelListViewModel.Channels;
			_parsingService.TelemetryParser.ItemsCollection = TelemetryLiveViewModel.Slots;
		}

		public async void LoadAvailableComPorts()
		{
			ComPorts = await ComPortInformation.GetAvailableComPorts();
			SelectedComPort = ComPorts.Where(p => p.DisplayName.Contains("Futaba")).FirstOrDefault();
		}

	}
}
